import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;

public class ProcessManager {
    private JFrame window;
    private JButton addProcessButton;
    private JButton deleteProcessButton;
    private JTextArea output;
    private ShareQueue SQ;

    public ProcessManager(){
        window = new JFrame("任务管理器");
        output = new JTextArea();
        addProcessButton = new JButton("添加进程");
        deleteProcessButton = new JButton("删除进程");

        window.setSize(500, 600);//设置容器尺寸
        window.setLocation(200, 200);//设置容器位置
        window.setLayout(null);
        output.setBounds(1, 0, 498, 400);
        output.setText("程序开始运行\n");
        output.append("时间片轮转算法");
        addProcessButton.setBounds(100, 400, 100, 30);//设置按钮在容器中的位置
        deleteProcessButton.setBounds(300, 400, 100, 30);//设置按钮在容器中的位置
        addProcessButtonInit();
        deleteProcessButtonInit();

        window.add(output);
        window.add(addProcessButton);
        window.add(deleteProcessButton);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//界面关闭后程序结束
        window.setVisible(true);//界面可视化。

        SQ = new ShareQueue();
    }

    public void addProcessButtonInit(){
        addProcessButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog dialog = new JDialog();
                dialog.setTitle("请输入");
                dialog.setSize(500, 200);
                dialog.setLayout(new FlowLayout());
                dialog.setLocationRelativeTo(null); // 居中显示对话框
                dialog.setModal(true); // 设置为模态对话框
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); // 设置关闭操作为dispose

                JLabel Name = new JLabel("进程名称");
                JTextField aName = new JTextField(5);//创建具有5列的文本框
                JLabel need = new JLabel("需要耗时");
                JTextField aNeed = new JTextField(5);//创建具有5列的文本框
                JButton aProcess = new JButton("确认添加");
                aProcess.setBounds(100, 100, 100, 30);
                aProcess.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String name = aName.getText();
                        int need = Integer.parseInt(aNeed.getText());
                        SQ.addTask(new Task(name, need));
                        dialog.dispose();
                    }
                });

                dialog.add(Name);
                dialog.add(aName);
                dialog.add(need);
                dialog.add(aNeed);
                dialog.add(aProcess);
                dialog.setVisible(true);
            }
        });

    }
    public void deleteProcessButtonInit(){
        deleteProcessButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog a = new JDialog();
                a.setTitle("请输入");
                a.setSize(500, 200);
                a.setLocation(200, 400);
                a.setLayout(new FlowLayout());

                JLabel Name = new JLabel("进程名称");
                JTextField dName = new JTextField(5);//创建具有5列的文本框
                JButton dProcess = new JButton("确认删除");
                dProcess.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String name = dName.getText();
                        for (Task t : SQ.readyQ) {
                            if (name.equals(t.name)) {
                                SQ.readyQ.remove(t);
                                break;
                            }
                        }
                        a.dispose();
                    }
                });
                a.add(Name);
                a.add(dName);
                a.add(dProcess);
                a.setVisible(true);
            }
        });
    }
    private void show(int i){
        output.setText("第" + i + "个时间片\n");
        output.append("名称\t优先级\tcpuTime\tneedTime\t状态\n");
        output.append(SQ.runQ.get(0).ToString("run"));
        for (Task t : SQ.readyQ) {
            output.append(t.ToString("wait"));
        }
        for(Task t : SQ.doneQ)
            output.append(t.ToString("done"));
    }

    public void run() {
        for (int i = 1; i <= 60 && !SQ.readyQ.isEmpty(); i++) {
            try {  //停顿一秒表示运行一个时间片
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            /*时间片开始选择优先级最高的进程进入运行队列*/
            SQ.runQ.add(SQ.readyQ.get(0));
            SQ.readyQ.remove(0);
            SQ.runQ.get(0).cpuTime++;

            // show info
            show(i);

            if (SQ.runQ.get(0).isDone()) {
                SQ.doneQ.add(SQ.runQ.get(0));
            }else{
                SQ.readyQ.add(SQ.runQ.get(0));
            }
            SQ.runQ.remove(0);

            //有新进程加入需要重新按优先级排列
            if (SQ.newAddProcessFlag) {
                Collections.sort(SQ.readyQ);
                SQ.newAddProcessFlag = false;
            }
        }
    }
}